package com.ruoyi.material.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.entity.ZnMaterialCategory;
import com.ruoyi.material.service.IZnMaterialCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 产品类型Controller
 * 
 * @author ruoyi
 * @date 2021-12-22
 */
@RestController
@RequestMapping("/material/materialcategory")
public class ZnMaterialCategoryController extends BaseController
{
    @Autowired
    private IZnMaterialCategoryService znMaterialCategoryService;

    /**
     * 查询产品类型列表
     */
    @PreAuthorize("@ss.hasPermi('material:materialcategory:list')")
    @GetMapping("/list")
    public AjaxResult list(ZnMaterialCategory znMaterialCategory)
    {
        znMaterialCategory.setDeleteFlag("0");
        znMaterialCategory.setTenantId(SecurityUtils.getUserId());
        List<ZnMaterialCategory> list = znMaterialCategoryService.selectZnMaterialCategoryList(znMaterialCategory);
        return AjaxResult.success(list);
    }
    /**
     * 查询产品类型列表下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(ZnMaterialCategory znMaterialCategory)
    {
        znMaterialCategory.setTenantId(SecurityUtils.getLoginUser().getUser().getTenantId());
        znMaterialCategory.setDeleteFlag("0");
        List<ZnMaterialCategory> znMaterialCategorys = znMaterialCategoryService.selectZnMaterialCategoryList(znMaterialCategory);
        return AjaxResult.success(znMaterialCategoryService.buildDeptTreeSelect(znMaterialCategorys));
    }
    /**
     * 导出产品类型列表
     */
    @PreAuthorize("@ss.hasPermi('material:materialcategory:export')")
    @Log(title = "产品类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZnMaterialCategory znMaterialCategory)
    {
        List<ZnMaterialCategory> list = znMaterialCategoryService.selectZnMaterialCategoryList(znMaterialCategory);
        ExcelUtil<ZnMaterialCategory> util = new ExcelUtil<ZnMaterialCategory>(ZnMaterialCategory.class);
        util.exportExcel(response, list, "产品类型数据");
    }

    /**
     * 获取产品类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('material:materialcategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(znMaterialCategoryService.selectZnMaterialCategoryById(id));
    }

    /**
     * 新增产品类型
     */
    @PreAuthorize("@ss.hasPermi('material:materialcategory:add')")
    @Log(title = "产品类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZnMaterialCategory znMaterialCategory)
    {
        if(StringUtils.isEmpty(znMaterialCategory.getName())){
            return AjaxResult.error("名称不能为空");
        }
        if(StringUtils.isEmpty(znMaterialCategory.getSerialNo())){
            return AjaxResult.error("编号不能为空");
        }
        znMaterialCategory.setDeleteFlag("0");
        znMaterialCategory.setTenantId(SecurityUtils.getLoginUser().getUser().getTenantId());
        znMaterialCategory.setCreateTime(new Date());
        znMaterialCategory.setUpdateTime(new Date());
        /*if(znMaterialCategory.getParentId()==null){
            znMaterialCategory.setCategoryLevel(znMaterialCategory.);
        }
*/
        ZnMaterialCategory znMaterialCategoryQuery=new ZnMaterialCategory();
        znMaterialCategoryQuery.setDeleteFlag("0");
        znMaterialCategoryQuery.setTenantId(SecurityUtils.getLoginUser().getUser().getTenantId());
        znMaterialCategoryQuery.setName(znMaterialCategory.getName());
       List<ZnMaterialCategory> znMaterialCategorys= znMaterialCategoryService.selectZnMaterialCategoryList(znMaterialCategoryQuery);
       if (znMaterialCategorys.size()>0){
           return AjaxResult.error("有相同名称");
       }
        return toAjax(znMaterialCategoryService.insertZnMaterialCategory(znMaterialCategory));
    }

    /**
     * 修改产品类型
     */
    @PreAuthorize("@ss.hasPermi('material:materialcategory:edit')")
    @Log(title = "产品类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZnMaterialCategory znMaterialCategory)
    {
        return toAjax(znMaterialCategoryService.updateZnMaterialCategory(znMaterialCategory));
    }

    /**
     * 删除产品类型
     */
    @PreAuthorize("@ss.hasPermi('material:materialcategory:remove')")
    @Log(title = "产品类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        if(ids==null||ids.length==0){
            return AjaxResult.error("ID不能为空");
        }
        for(Long id:ids){
            ZnMaterialCategory znMaterialCategoryQuery=new ZnMaterialCategory();
            znMaterialCategoryQuery.setParentId(id);
            List<ZnMaterialCategory> znMaterialCategorys=znMaterialCategoryService.selectZnMaterialCategoryList(znMaterialCategoryQuery);
            if(znMaterialCategorys.size()>0){
                return AjaxResult.error("还有子项，请先删除子项");
            }
            ZnMaterialCategory znMaterialCategoryUpdate=new ZnMaterialCategory();
            znMaterialCategoryUpdate.setId(id);
            znMaterialCategoryUpdate.setDeleteFlag("1");
            znMaterialCategoryService.updateZnMaterialCategory(znMaterialCategoryUpdate);
        }

        return AjaxResult.success("删除成功");
    }
}
